package com.xiyoufang.aij.circle.handler;

import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.xiyoufang.aij.circle.event.CreateGroupEvent;
import com.xiyoufang.aij.circle.event.InviteEvent;
import com.xiyoufang.aij.circle.response.CreateGroupEventResponse;
import com.xiyoufang.aij.circle.response.InviteEventListResponse;
import com.xiyoufang.aij.circle.response.InviteEventResponse;
import com.xiyoufang.aij.core.AiJCoreDb;
import com.xiyoufang.aij.core.AiJDbPro;
import com.xiyoufang.aij.core.AppConfig;
import com.xiyoufang.aij.core.ResponseFactory;
import com.xiyoufang.aij.handler.EventHandler;
import com.xiyoufang.aij.utils.Json;
import org.joda.time.DateTime;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.websocket.common.WsResponse;

import java.util.ArrayList;
import java.util.List;

public class CreateGroupHandler extends EventHandler<CreateGroupEvent> {

    public CreateGroupHandler() {
        super(CreateGroupEvent.class);
    }

    @Override
    protected void handle(CreateGroupEvent event, ChannelContext channelContext) {
        AiJDbPro uc = AiJCoreDb.uc();
        Record record = new Record().set("user_id", event.getUserId()).
                set("circle_name", event.getGroupName())
                .set("description", event.getDesc())
                .set("created_time", DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        boolean circle = AiJCoreDb.uc().save("circle", record);
        CreateGroupEventResponse groupEventResponse = new CreateGroupEventResponse();
        groupEventResponse.setFlag(circle);
        Tio.send(channelContext, WsResponse.fromText(Json.toJson(groupEventResponse), AppConfig.use().getCharset()));

    }
}
